/*
 * UserListDialog.java
 *
 * Created on September 8, 2006, 2:04 AM
 */

package com.floreantpos.ui.dialog;

import java.awt.event.ActionEvent;
import java.util.List;

import com.floreantpos.model.Member;
import com.floreantpos.model.dao.MemberDAO;
import com.floreantpos.swing.ListComboBoxModel;
import com.floreantpos.ui.views.NoteView;
import com.floreantpos.ui.views.MemberSelectionView;
import com.floreantpos.main.Application;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;

/**
 *
 * @author  MShahriar
 */
public class SelectMemberDialog extends POSDialog {
    
    private MemberSelectionView memberSelectionView;
    private NoteView noteView;
    
    private List<Member> memberList;
    /** Creates new form UserListDialog 
     */
    public SelectMemberDialog(boolean modal) {
        super(Application.getPosWindow(), modal, false);
        setCanceled(true);
        
        initComponents();
        //pos constant
        
        setTitle("Member Selection");
        readRecentMembers();
        
        
        
    }
    private void readRecentMembers()
    {
        memberList = MemberDAO.instance.getClockedInMember();
        memberSelectionView.setMemberList(memberList);
    }
    private void searchMemberLastName(String value)
    {
        memberList = MemberDAO.instance.findMemberByLname(value);
        memberSelectionView.setMemberList(memberList);
    }
    private void searchMemberName(String value)
    {
        List<Member> tmpList;
        List<Member> memList = new ArrayList<Member>();
        tmpList = MemberDAO.instance.findAll();
        for (Member m:tmpList)
        {
            
            if (m.getLname().toLowerCase().contains(value.toLowerCase()) ||
                    m.getFname().toLowerCase().contains(value.toLowerCase()))
                memList.add(m);
        }
        memberList = memList;
        memberSelectionView.setMemberList(memberList);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        memberSelectionView = new MemberSelectionView();
        noteView = new NoteView();
        
        titlePanel1 = new com.floreantpos.ui.TitlePanel();
        transparentPanel1 = new com.floreantpos.swing.TransparentPanel();
        transparentPanel2 = new com.floreantpos.swing.TransparentPanel();
        btnOk = new com.floreantpos.swing.PosButton();
        btnSearch = new com.floreantpos.swing.PosButton();
        btnCancel = new com.floreantpos.swing.PosButton();
        jSeparator1 = new javax.swing.JSeparator();
        transparentPanel3 = new com.floreantpos.swing.TransparentPanel();
        

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        titlePanel1.setTitle(com.floreantpos.POSConstants.SELECT_USER);
        //getContentPane().add(titlePanel1, java.awt.BorderLayout.NORTH);
        
        
        
        transparentPanel1.setLayout(new java.awt.BorderLayout());

        btnOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/finish_32.png")));
        btnOk.setText(com.floreantpos.POSConstants.OK);
        btnOk.setPreferredSize(new java.awt.Dimension(120, 50));
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                if (memberSelectionView.getMember() != null)
                    doOk(evt);
            }
        });


        transparentPanel2.add(btnOk);
        
        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/finish_32.png")));
        btnSearch.setText("Search");  //pos constant
        btnSearch.setPreferredSize(new java.awt.Dimension(120, 50));
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doSearch(evt);
            }
        });

        transparentPanel2.add(btnSearch);

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel_32.png")));
        btnCancel.setText(com.floreantpos.POSConstants.CANCEL);
        btnCancel.setPreferredSize(new java.awt.Dimension(120, 50));
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doCancel(evt);
            }
        });

        
        
        transparentPanel2.add(btnCancel);
            
        
        transparentPanel1.add(noteView,java.awt.BorderLayout.NORTH);
        transparentPanel1.add(memberSelectionView,java.awt.BorderLayout.CENTER);

        transparentPanel1.add(transparentPanel2, java.awt.BorderLayout.SOUTH);

        //transparentPanel1.add(jSeparator1, java.awt.BorderLayout.NORTH);

        getContentPane().add(transparentPanel1, java.awt.BorderLayout.SOUTH);

        ;

        org.jdesktop.layout.GroupLayout transparentPanel3Layout = new org.jdesktop.layout.GroupLayout(transparentPanel3);
        transparentPanel3.setLayout(transparentPanel3Layout);
        transparentPanel3Layout.setHorizontalGroup(
            transparentPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(transparentPanel3Layout.createSequentialGroup()
                .addContainerGap()
                
                .addContainerGap())
        );
        transparentPanel3Layout.setVerticalGroup(
            transparentPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(transparentPanel3Layout.createSequentialGroup()
                .addContainerGap()
                
                .addContainerGap(13, Short.MAX_VALUE))
        );
        getContentPane().add(transparentPanel3, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>                        

    private void doOk(java.awt.event.ActionEvent evt) {
        Member member = this.getSelectedMember();
        Calendar currentTime = Calendar.getInstance();
        if (!member.getClockedIn())
            member.doClockIn(currentTime);
    	setCanceled(false);
    	dispose();
    }                     
     private void doSearch(java.awt.event.ActionEvent evt) {
         String noteStr = noteView.getNote();
        if (!(noteStr == null && noteStr == ""))
        {
            this.searchMemberName(noteStr);
        }
    }  
    private void doCancel(java.awt.event.ActionEvent evt) {                          
    	setCanceled(true);
    	dispose();
    }                         
    
    public Member getSelectedMember() {
    	return (Member) memberSelectionView.getMember();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UserListDialog(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify                     
    private com.floreantpos.swing.PosButton btnCancel;
    private com.floreantpos.swing.PosButton btnOk;
    private com.floreantpos.swing.PosButton btnSearch;
    
    private javax.swing.JSeparator jSeparator1;
    private com.floreantpos.ui.TitlePanel titlePanel1;
    private com.floreantpos.swing.TransparentPanel transparentPanel1;
    private com.floreantpos.swing.TransparentPanel transparentPanel2;
    private com.floreantpos.swing.TransparentPanel transparentPanel3;
    // End of variables declaration                   

    
    
}
